#! /bin/bash

set -e

work_dir=$(dirname $(realpath $BASH_SOURCE))

function main(){
  local instances=$(vultr-cli instance list | tail -n +2 | head -n -3);
  local instance_count=$(echo "$instances" | wc -l)

  echo $instance_count
  
  if [ $instance_count -ne 1 ] 
  then
    echo More than 1 instance
    echo "$instances";
    exit 1;
  fi

  local instance_id=$(echo $instances | awk '{ print $1 }');
  local instance_ip=$(echo $instances | awk '{ print $2 }');

  $work_dir/gateway-wg-stop || true;

  echo Deleting
  echo $instance_id;
  echo $instance_ip;

  vultr-cli instance delete $instance_id;
}

main $@

